﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Services.Notification
{
    public class Notification
    {
        Web.Model.HangoutDBEntities context = new Web.Model.HangoutDBEntities();

        public void MarkAllAsRead(int userId, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(userId, accesstoken, context))
                {
                    Web.Core.Notifications.Notification.MarkAllNotificationsAsRead(userId, ref context);
                }
                else
                {
                    throw new Web.Core.Exceptions.Account.UnAuthorisedAccess();
                }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
            }
        }


        public List<Services.Objects.Notification> GetNotifications(int userId, int take, int skip, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(userId, accesstoken, context))
                {
                    return Web.Core.Notifications.Notification.GetNotifications(userId, take, skip, context).Select(o=>new Services.Objects.Notification{DatetimePosted=o.DateTimeStamp,MetaData=o.MetaData,NotificationID=o.NotificationID,Text=o.Text}).ToList();
                }
                else
                {
                    throw new Web.Core.Exceptions.Account.UnAuthorisedAccess();
                }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }

        public Hangout.Web.Model.Notification GetNotification(int notificationId, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(accesstoken, context))
                {
                    return Web.Core.Notifications.Notification.GetNotification(notificationId, context);
                }
                else
                {
                    throw new Web.Core.Exceptions.Account.UnAuthorisedAccess();
                }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(0, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }

        }
    }
}